<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		<script src="../js/jquery-3.5.1.js" type="text/javascript" charset="utf-8"></script>
		<form >
		<input type="text" name="name"placeholder="书本名称" /><br />
		<input type="text" name="price"placeholder="价格" /><br />
		<input type="date" name="date"placeholder="出版日期" /><br />
		<button type="button" onclick="save()">保存</button>
		</form>
	</body>
	<script type="text/javascript">
		
		let user1=sessionStorage.getItem("user");
		
			let edit=JSON.parse(sessionStorage.getItem("editname"));
			if (edit) {
				
				$("input[name='name']").val(edit.name);
				$("input[name='price']").val(edit.price);
				$("input[name='date']").val(edit.date);
				
			}
			
		function save(){
			let  name =$("input[name='name']").val();
			let  price =$("input[name='price']").val();
			let  date =$("input[name='date']").val();
			let data=JSON.parse(localStorage.getItem("data"));
			//id自增长
			let tem=0;
			for(let item of data){
				let id1=parseInt(item.id);
				if (tem<id1) {
					tem=id1;
				}
			}
			
			let obj={
				id:tem+1,
				name:name,
				price:price,
				date:date,
				count:edit ? edit.count : 1,
				user:sessionStorage.getItem("user")
			}
			if (data) {
				for (let item of data) {
				if (edit) {
					if (edit.name!==name&&item.name===name) {
						alert("书名不能重复！");
						return;
					}
				} else{
					if (item.name===name) {
						alert("书名不能重复！");
						return;
					}
				}
					
				}
				if (name&&price&&date) {
					
			data.push(obj);
			localStorage.setItem("data", JSON.stringify(data));
			// console.log(data);
			sessionStorage.removeItem("editname");
			location.href="index.html";
				}else{
					alert("输入完整信息");
				}
			}
		}
	</script>
</html>
